package com.jerei.plugin.control;

import com.alibaba.fastjson.JSONArray;
import com.jerei.plugin.entity.WcmCommonRole;
import com.jerei.plugin.service.IRoleMenuService;
import com.jerei.plugin.service.IWcmCommonGroupService;
import com.jerei.plugin.service.IWcmCommonRoleService;
import com.jerei.web.plugin.controller.PluginController;
import com.jerei.web.utils.UtilsWeb;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import ro.fortsoft.pf4j.Extension;

/**
 * Created by sunfeihu on 2016/12/12.
 */
@Extension
public class RoleControl extends PluginController {

    @Autowired
    private IWcmCommonRoleService iWcmCommonRoleService;
    @Autowired
    private IRoleMenuService iRoleMenuService;
    @Autowired
    private IWcmCommonGroupService iWcmCommonGroupService;

    @RequiresPermissions({"common-role:view"})
    public String groupTreeAll() {
        Long gid = UtilsWeb.getLong(this.getRequest(), "gid");
        JSONArray array = iWcmCommonRoleService.allGroupRoles(gid);
        if (null == array) {
            return new JSONArray().toJSONString();
        }
        return array.toJSONString();
    }

    @RequiresPermissions({"common-menu:view"})
    public String groupPageMenuTree() {
        Long gid = UtilsWeb.getLong(this.getRequest(), "gid");
        Long roleId = UtilsWeb.getLong(this.getRequest(), "roleId");
        if (null == roleId) {
            return new JSONArray().toJSONString();
        }
        return iRoleMenuService.treeGroupMenu(roleId, gid);
    }

    @RequiresPermissions({"common-role:view"})
    public void roleList() {
        Long gid = UtilsWeb.getLong(this.getRequest(), "gid");
        this.getModel().addAttribute("group", iWcmCommonGroupService.find(gid));
    }

    @RequiresPermissions({"common-role:save", "common-role:update", "common-role:view"})
    public void roleEdit() {
        Long id = UtilsWeb.getLong(this.getRequest(), "id");
        WcmCommonRole entity = null;
        if (null != id) {
            entity = iWcmCommonRoleService.find(id);
        } else {
            Long pid = UtilsWeb.getLong(this.getRequest(), "pid");
            Long gid = UtilsWeb.getLong(this.getRequest(), "gid");
            entity = new WcmCommonRole();
            entity.setGid(gid);
            entity.setMenuId(2L);
            if (null != pid) {
                entity.setPid(pid);
            }
        }
        this.getModel().addAttribute("obj", entity);
    }

}
